package com.weblearning.tliasweb.controller;

import com.weblearning.tliasweb.pojo.Emp;
import com.weblearning.tliasweb.pojo.Result;
import com.weblearning.tliasweb.service.EmpService;
import com.weblearning.tliasweb.utils.JwtUtils;
import io.jsonwebtoken.Jwts;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController

public class LoginController {

    @Autowired
    private EmpService empService;

    @PostMapping("/login")
    public Result login(@RequestBody Emp emp){
        log.info("登录验证"+emp);
          Emp e = empService.login(emp);
        if(e != null){
            Map<String,Object> p=new HashMap<>();
            p.put("id",e.getId());
            p.put("name",e.getName());
            p.put("username",e.getUsername());
            String s = JwtUtils.generateJwt(p);
            return  Result.success(s);
        }
        else{
            return Result.error("登录失败");
        }
    }
}
